from create_app import create_app
from settings.config import DevConfig, DefaultConfig
from flask import *
from flask_restful import reqparse
from utils.JwtTool import JwtTool
from utils.RongTool import RongTool

app = create_app(DevConfig)

@app.before_request
def gz():
    print('>>>>>>挂载已执行')
    req = reqparse.RequestParser()
    req.add_argument('token', default='', location='headers')
    req.add_argument('long_token')
    args = req.parse_args()
    # print('+++', args['token'])

    # TODO 判断token是否存在
    ser = RongTool().r.get(args['token'])

    payload = JwtTool().jwt_payload(args['token'])
    long_payload = JwtTool().jwt_payload(args['long_token'])
    print('payload:', payload)

    if not payload:
        g.id = 0
    elif ser:
        g.id = -1
    elif payload == 1:
        g.id = -2
    else:
        g.id = payload['id']
    g.long = long_payload